package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bdelegate.BdelegateWeb;
import beans.MozoBean;

/**
 * Servlet implementation class Logeo
 */
public class Logeo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Logeo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String user= request.getParameter("user");
		String pass= request.getParameter("pass");
		String id= request.getParameter("idSucursal");
		String jspPage = null;
		MozoBean m= new MozoBean();
		BdelegateWeb bd = new BdelegateWeb();
		
		if ((user.equals("2001")||user.equals("2002")||user.equals("2003") ) && (pass.equals("m"))){
			Integer us= Integer.parseInt(user);
			MozoBean mozologeo= bd.getInstancia().obtenerMozo(us);
			if (mozologeo.getLegajo()!=0){
			request.getSession().setAttribute("mozologeo", mozologeo);
			jspPage = "/terMozos/home.jsp";	}
		}
		else if ((user.equals("c")) && (pass.equals("c"))){
			jspPage = "/terCajero/home.jsp";	}
		else{ 
			request.setAttribute("idSucursal", id);
			jspPage = "./error.jsp";
		}
		 dispatch(jspPage, request, response);
	}
	protected void dispatch(String jsp, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        if (jsp != null)
        {
        	/*Envia el control al JSP que pasamos como parametro, y con los 
        	 * request / response cargados con los parametros */
        	RequestDispatcher rd = request.getRequestDispatcher(jsp);
            rd.forward(request, response);
        }
    }
}
